﻿using System;
using Store.Model.Models;
using System.Linq;
using Store.ViewModel;
using System.Collections.Generic;

namespace Store.Dal
{
    public class EmployeeRegisterDal
    {
        private readonly StoreContext Db;
        public EmployeeRegisterDal(StoreContext _db)
        {
            Db = _db;
        }
        //员工的登录
        public int Login(string EmployeeAccount, string PassWords)
        {
            var data = from s in Db.Employees
                       where s.EmployeeAccount.Equals(EmployeeAccount) && s.PassWords.Equals(PassWords)
                       select s;
            if (data.Count()>0)
            {
                return 1;
            }
            return -1;
        }
        //查询员工权限下的菜单信息
        public List<EmployeeMenu> GetEmployeeMenus()
        {
            var data = from s in Db.Positions
                       join r in Db.Relations on s.Pid equals r.Pid
                       join m in Db.Menus on r.Mid equals m.Mid
                       where s.Pid.Equals(1)
                       select new EmployeeMenu
                       {
                           Mname = m.Mname,
                           Url = m.Url
                       };
            return data.ToList();
        }
        //添加员工信息
        public int EmployeeAdd(Employee emp)
        {
            Db.Employees.Add(emp);
            return Db.SaveChanges();
        }
        //显示员工信息和分页
        public List<EmployeeModel> GetEmployee(string AccountName, string Pid, int PageIndex, int PageSize, out int total)
        {
            var data = (from s in Db.Employees
                        join p in Db.Positions on s.Pid equals p.Pid
                        where s.EmpState.Equals(1)
                        orderby s.EmployeeId
                        select new EmployeeModel()
                        {
                            EmployeeId = s.EmployeeId,
                            EmployeeAccount = s.EmployeeAccount,
                            EmployeeName = s.EmployeeName,
                            EmployeePhone = s.EmployeePhone,
                            Email = s.Email,
                            PositionName = p.PositionName,
                            RegisterTime = s.RegisterTime,
                            States = s.States,
                            Pid = s.Pid,
                            PassWords = s.PassWords,
                            RemarkMessage = s.RemarkMessage,
                            Sex = s.Sex

                        }).ToList();
            if (AccountName != null)
            {
                data = data.Where(s => s.EmployeeAccount.Equals(AccountName) || s.EmployeeName.Equals(AccountName)).ToList();
            }

            if (Pid != null)
            {
                data = data.Where(s => s.Pid.Equals(Convert.ToInt32(Pid))).ToList();
            }
            total = data.Count();
            return data.Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList();
        }
        //删除员工信息
        public int DeleteEmployee(int id)
        {
            Employee data = Db.Employees.Find(id);
            if (data != null)
            {
                data.EmpState = 0;
                return Db.SaveChanges();
            }
            return -1;
        }
        //修改员工信息
        public int EditEmployee(Employee emp)
        {
            Employee data = Db.Employees.Find(emp.EmployeeId);
            if (data != null)
            {
                data.Sex = emp.Sex;
                data.EmployeeAccount = emp.EmployeeAccount;
                data.EmployeeName = emp.EmployeeName;
                data.EmployeePhone = emp.EmployeePhone;
                data.Email = emp.Email;
                data.Pid = emp.Pid;
                data.PassWords = emp.PassWords;
                data.RemarkMessage = emp.RemarkMessage;
                return Db.SaveChanges();
            }
            return -1;
        }
        //显示角色信息
        public List<Position> GetPositions(int PageIndex, int PageSize, out int total)
        {
            var data = from s in Db.Positions
                       where s.PostState.Equals(1)
                       orderby s.Pid
                       select s;
            total = data.Count();
            return data.Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList();
        }
        //绑定下拉框
        public List<Position> GetPositionSelect()
        {
            var data = from s in Db.Positions
                       select s;
            return data.ToList();
        }
        //添加角色信息
        public int AddPositions(Position p)
        {
            var data = from s in Db.Positions
                       select new {s.PositionName };
            foreach (var item in data)
            {
                if (p.PositionName.Equals(item.PositionName))
                {
                    return -1;
                }
            }

            Db.Positions.Add(p);
            return Db.SaveChanges();
        }
        //删除角色信息(修改状态，假删)
        public int DeletePositions(int id)
        {
            Position data = Db.Positions.Find(id);
            if (data != null)
            {
                data.PostState = 0;
                return Db.SaveChanges();
            }
            return -1;
        }
        //修改角色信息
        public int EditPositions(Position s)
        {
            Position data = Db.Positions.Find(s.Pid);
            if (data != null)
            {
                data.PositionName = s.PositionName;
                data.Pstate = s.Pstate;
                data.Describe = s.Describe;
                return Db.SaveChanges();
            }
            return -1;
        }
    }
}
